package com.atguigu.controller;

import com.alipay.api.internal.util.AlipaySignature;
import com.atguigu.config.AlipayConfig;
import com.atguigu.entity.PaymentInfo;
import com.atguigu.enums.PaymentStatus;
import com.atguigu.enums.PaymentType;
import com.atguigu.result.RetVal;
import com.atguigu.service.PaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/payment")
public class PaymentInfoController {

    @Autowired
    PaymentInfoService paymentInfoService;
    //1.返回支付二维码页面信息
    //http://api.gmall.com/payment/createQrCode/1
    @RequestMapping("/createQrCode/{orderId}")
    public String createQrCode(@PathVariable Long orderId) throws Exception {
        String from = paymentInfoService.createQrCode(orderId);
        return from;
    }

    //2.发起异步通知
    //2.完成异步回调
    @PostMapping("/async/notify")
    public String asyncNotify(@RequestParam Map<String,String> paramMap) throws Exception {
        System.out.println("支付宝异步调我的接口啦");
        boolean signVerified = AlipaySignature.rsaCheckV1(paramMap, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type);
        String tradeStatus = paramMap.get("trade_status");//交易状态
        String outTradeNo = paramMap.get("out_trade_no");//订单交易编号
        if(signVerified){
            //交易成功或者完成才能更改支付订单信息
            if ("TRADE_SUCCESS".equals(tradeStatus) || "TRADE_FINISHED".equals(tradeStatus)){
                PaymentInfo paymentInfo = paymentInfoService.getPaymentInfo(outTradeNo, PaymentType.ALIPAY.name());
                //如果交易已经关闭或者已经支付 防止支付宝异步重复调用时继续操作
                if (paymentInfo.getPaymentStatus().equals(PaymentStatus.ClOSED.name())||
                        paymentInfo.getPaymentStatus().equals(PaymentStatus.PAID.name())){
                    return "failure";
                }
                //修改支付订单信息
                paymentInfoService.updatePaymentInfo(outTradeNo,PaymentType.ALIPAY.name(),paramMap);
                return "success";
            }
        }else{
            //TODO 验签失败则记录异常日志，并在response中返回failure.
            return "failure";
        }
        return "failure";
    }

    //3.发起退款 http://127.0.0.1:8005/payment/refund/28
    @RequestMapping("/refund/{orderId}")
    public RetVal refund(@PathVariable Long orderId) throws Exception{
        boolean flag = paymentInfoService.refund(orderId);
        return RetVal.ok(flag);
    }

    //4.关闭交易接口 http://127.0.0.1:8005/payment/closeAlipayTrade/28
    @GetMapping("/closeAlipayTrade/{orderId}")
    public boolean closeAlipayTrade(@PathVariable Long orderId) throws Exception{
        return paymentInfoService.closeAlipayTrade(orderId);
    }


    //5.查询阿里内部订单信息 http://127.0.0.1:8005/payment/queryAlipayTrade/28
    @GetMapping("/queryAlipayTrade/{orderId}")
    public boolean queryAlipayTrade(@PathVariable Long orderId) throws Exception{
        return paymentInfoService.queryAlipayTrade(orderId);
    }

    //5.查询支付订单信息
    @GetMapping("/getPaymentInfo/{outTradeNo}")
    public PaymentInfo getPaymentInfo(@PathVariable String outTradeNo){
        return paymentInfoService.getPaymentInfo(outTradeNo, PaymentType.ALIPAY.name());
    }



}
